# Copyright (C) 2023 - 2024 Advanced Micro Devices, Inc.  All rights reserved.
# SPDX-License-Identifier: MIT
cmake_minimum_required(VERSION 3.14.7)
include(${CMAKE_CURRENT_SOURCE_DIR}/Memory_testsExample.cmake)
include(${CMAKE_CURRENT_SOURCE_DIR}/UserConfig.cmake)
set(APP_NAME memory_tests)
project(${APP_NAME})

find_package(common)
enable_language(C ASM)
collect(PROJECT_LIB_DEPS xilstandalone)
collect(PROJECT_LIB_DEPS xil)
collect(PROJECT_LIB_DEPS xiltimer)
collect(PROJECT_LIB_DEPS gcc)
collect(PROJECT_LIB_DEPS c)
collector_list (_deps PROJECT_LIB_DEPS)
list (APPEND _deps ${USER_LINK_LIBRARIES})

MACRO(memory_section index ddrip start_address size)
     set(MEMNODE${index}
         "{
        \t\"${ddrip}\",
        \t\"${ddrip}\",
        \t${start_address},
        \t${size},
          },\n\t ")
endmacro()

list(LENGTH TOTAL_MEM_CONTROLLERS _len)
MATH(EXPR _len "${_len}-1")
set(NUM_MEM_RANGES ${_len})
set(index 0)
foreach(MEM_CONTROLLER ${TOTAL_MEM_CONTROLLERS})
    set(val ${${MEM_CONTROLLER}})
    list(GET val 0 start_address)
    list(GET val 1 size)
    memory_section(${index} ${MEM_CONTROLLER} ${start_address} ${size})
    if (${index} EQUAL 0)
        set(linker_mem ${MEM_CONTROLLER})
    endif()
    MATH(EXPR index "${index}+1")
endforeach()
foreach(i RANGE ${index})
    if (${i} EQUAL 0)
        string(APPEND MEMNODES "/* ")
        string(APPEND MEMNODES ${linker_mem})
        string(APPEND MEMNODES " memory will not be tested since application resides in the same memory */\n\t")
    else()
        string(APPEND MEMNODES ${MEMNODE${i}})
    endif()
endforeach()
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/memory_config_g.c.in ${CMAKE_CURRENT_SOURCE_DIR}/memory_config_g.c)
if (CMAKE_EXPORT_COMPILE_COMMANDS AND
    (NOT ${YOCTO}))
    set(CMAKE_CXX_STANDARD_INCLUDE_DIRECTORIES ${CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES})
    set(CMAKE_C_STANDARD_INCLUDE_DIRECTORIES ${CMAKE_C_IMPLICIT_INCLUDE_DIRECTORIES})
endif()
collect (PROJECT_LIB_SOURCES platform.c)
collect (PROJECT_LIB_SOURCES memorytest.c)
collect (PROJECT_LIB_SOURCES memory_config_g.c)
collector_list (_sources PROJECT_LIB_SOURCES)
set(CMAKE_INFILE_PATH "${CMAKE_SOURCE_DIR}/linker_files/")
linker_gen(${CMAKE_INFILE_PATH})
string(APPEND CMAKE_C_FLAGS ${USER_COMPILE_OPTIONS})
string(APPEND CMAKE_CXX_FLAGS ${USER_COMPILE_OPTIONS})
string(APPEND CMAKE_C_LINK_FLAGS ${USER_LINK_OPTIONS})
string(APPEND CMAKE_CXX_LINK_FLAGS ${USER_LINK_OPTIONS})
add_dependency_on_bsp(_sources)
add_executable(${APP_NAME}.elf ${_sources})
set_target_properties(${APP_NAME}.elf PROPERTIES LINK_DEPENDS ${USER_LINKER_SCRIPT})
target_link_libraries(${APP_NAME}.elf -Wl,-T -Wl,\"${USER_LINKER_SCRIPT}\" -L\"${CMAKE_SOURCE_DIR}/\" -L\"${CMAKE_LIBRARY_PATH}/\" -L\"${USER_LINK_DIRECTORIES}/\" -Wl,--start-group,-l${_deps} -Wl,--end-group)
target_compile_definitions(${APP_NAME}.elf PUBLIC ${USER_COMPILE_DEFINITIONS})
target_include_directories(${APP_NAME}.elf PUBLIC ${USER_INCLUDE_DIRECTORIES})
print_elf_size(CMAKE_SIZE ${APP_NAME})
